package com.dh;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ESTestClient {
    private static RestClientBuilder builder;
    public static final String USERNAME = "elastic";
    public static final String PASSWORD = "Shangjian@123";
    public static final String SYSURI = "dhvm1";
    public static final int PORT = 9200;

    public static int CONNECT_TIMEOUT_MILLIS = 10000;
    public static int SOCKET_TIMEOUT_MILLIS = 30000;
    public static int CONNECTION_REQUEST_TIMEOUT_MILLIS = 5000;
    public static int MAX_CONN_PER_ROUTE = 10;
    public static int MAX_CONN_TOTAL = 30;

    private static RestHighLevelClient client;



    private static RestClientBuilder getHttpRequestBuild(String kmuri, int kmPort) {
        String[] split = kmuri.split(",");
        List<String> strings = Arrays.asList(split);
        List<HttpHost> list = new ArrayList<>();
        for (String string : strings) {
            HttpHost httpHost = new HttpHost(string, kmPort);
            list.add(httpHost);
        }
        builder = RestClient.builder(list.toArray(new HttpHost[list.size()]));
        // 设置用户名密码认证
        if (!StringUtil.isBlank(USERNAME) && !StringUtil.isBlank(PASSWORD)) {
            String auth = Base64.encodeBase64String((USERNAME+":"+PASSWORD).getBytes());
            builder.setDefaultHeaders(new BasicHeader[]{new BasicHeader("Authorization", "Basic " + auth)});
        }
        return builder;
    }

    // 配置连接时间延时
    public static void setConnectTimeOutConfig() {
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {

            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(CONNECT_TIMEOUT_MILLIS);
                requestConfigBuilder.setSocketTimeout(SOCKET_TIMEOUT_MILLIS);
                requestConfigBuilder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT_MILLIS);
                return requestConfigBuilder;
            }
        });
    }

    // 使用异步httpclient时设置并发连接数
    public static void setMutiConnectConfig() {
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.setMaxConnTotal(MAX_CONN_TOTAL);
                httpClientBuilder.setMaxConnPerRoute(MAX_CONN_PER_ROUTE);
                return httpClientBuilder;
            }
        });
    }

    public static RestHighLevelClient getClient() {
        if(null == builder) {
            builder = getHttpRequestBuild(SYSURI,PORT);
            setConnectTimeOutConfig();
            setMutiConnectConfig();
        }
        if(null == client) {
            client = new RestHighLevelClient(builder);
        }
        return client;
    }

    public static void closeClient() {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        client = null;
    }

    public static void main(String[] args) throws IOException {

        builder = getHttpRequestBuild(SYSURI,PORT);
        setConnectTimeOutConfig();
        setMutiConnectConfig();
//        restClient = builder.build();
        RestHighLevelClient client = new RestHighLevelClient(builder);

        System.out.println(client);
        client.close();
    }
}
